// 获取全局对象
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    imgUrls: [
      "https://mall.s.maizuo.com/15b20a45b268d8313727ee633b490e3f.jpg",
      "https://mall.s.maizuo.com/b2e031bb7a84668df5860ef2f1a873e2.jpg",
      "https://mall.s.maizuo.com/e4f0940a6cc7975380a2d0ca5cf6f0e0.jpg"
    ],
    goodsList: [],
    categoryList: [],
    active: 0
  },

  // 遮罩层
  showClass() {
    this.setData({
      show: !this.data.show
    })
  },

  // 阻止默认事件
  stop(event) {
    // event.preventDefault();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCategory();
  },

  // 获取categoryId
  getCategory() {
    let url = "/product/category";
    app.$http.get(url).then(res => {
      // console.log("分类", res);
      this.setData({
        categoryList: res.list
      })

      // 页面第一次加载默认Id
      let categoryId = res.list[0]._id;
      this.getList(categoryId);
    })
  },

  // 分类导航事件
  category(event) {
    console.log(event);

    let title = event.detail.title || event.currentTarget.dataset.title;
    this.setData({
      active: event.detail.index || event.currentTarget.dataset.index || 0
    })
    // 得到点击的分类
    let cate = this.data.categoryList.find(item => {
      return item.name === title;
    })
    this.getList(cate._id);
  },

  // 获取列表数据
  getList(categoryId) {
    let url = '/product/list';
    app.$http.get(url, { categoryId }, true).then(res => {
      // console.log("商品列表", res);
      this.setData({
        goodsList: res.list
      })
    })
  }
})